<style>
    .dn {
        display: none;
    }

    .attachments {
        display: block;
        float: left;
        overflow: hidden;
        text-align: center;
    }

    .option-item {
        overflow: hidden;
        margin-top: 5px;
        color: #1E9FFF;
        text-align: center;
    }

    .preview {
        cursor: pointer;
    }

    .file-a {
        font-size: 12px;
        word-break: break-all;
        text-overflow: ellipsis;
        overflow: hidden;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        margin-top: 5px;
    }

    .cardU {
        background-image: url(../../assets/images/card_u.png);
        background-repeat: no-repeat;
        background-position: center;
        width: calc(100% - 152px);
        height: 150px;
        padding: 0;
    }

    .cardU img {
        width: 100%;
        height: 100%;
    }
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">名称：</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="name" class="layui-input" type="text" placeholder="请输入名称"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">编码：</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="code" class="layui-input" type="text" placeholder="请输入编码"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog"
                                            lay-submit layui-form-keyDownSearch="true">查询
                                    </button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置
                                    </button>
                                </div>
                            </div>
                            <div class="query-rt"></div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
<!--                            <button id="warehouseBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>-->
                        </div>
                        <div class="lay-btn-rt">
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="warehouseTable" lay-filter="warehouseTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="warehouseTableBar">
<!--    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>-->
<!--    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>-->
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="warehouseForm">
    <form lay-filter="warehouseForm" class="layui-form model-form">
        <input name="id" type="hidden"/>


        <div class="layui-form-item">
            <div class="layui-col-md6">
                <label class="layui-form-label">产品名称</label>
                <div class="layui-input-block">
                    <select  id="productTypeId" name="productTypeId" lay-filter="productTypeId" lay-search></select>
                </div>
            </div>
            <div class="layui-col-md6">
                <label class="layui-form-label">产品编号</label>
                <div class="layui-input-block">
                    <input name="custodyPriceCode" placeholder="请输入类型编号" type="text" class="layui-input" required/>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-col-md6">
                <label class="layui-form-label">托管价格</label>
                <div class="layui-input-block">
                    <input name="price" placeholder="请输入价格" type="text" class="layui-input"
                           lay-verify="price" required/>
                </div>
            </div>
            <div class="layui-col-md6">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <input name="remark" placeholder="请输入备注" type="text" class="layui-input" lay-verify=""
                           required/>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-col-md6">
                <label class="layui-form-label">是否接单</label>
                <div class="layui-input-block">
<!--                    <input name="custodyPriceType" placeholder="请输入序号" type="text" class="layui-input"-->
<!--                           lay-verify="custodyPriceType" required/>-->
                    <select name="custodyPriceType">
                        <option value="">请选择</option>
                        <option value="0">是</option>
                        <option value="1">否</option>
                    </select>
                </div>
            </div>
            <div class="layui-col-md6" style="display: none">
                <label class="layui-form-label">产品名称</label>
                <div class="layui-input-block">
                    <input  id="custodyPriceName" name="custodyPriceName" lay-filter="custodyPriceName" lay-search/>
                </div>
            </div>
        </div>

        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="warehouseFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>

    var $, config;

    function stopBubbling(e) {
        e = window.event || e;
        if (e.stopPropagation) {
            e.stopPropagation();      //阻止事件 冒泡传播
        } else {
            e.cancelBubble = true;   //ie兼容
        }
    }

    //下载文件
    function downloadFile(realName, url) {
        stopBubbling(this);
        var $downForm = $("<form method='get'></form>");
        $downForm.attr("action", config.base_server + "api-file/downloadFile");
        var $input = $("<input type='hidden'>");
        $input.attr("name", "url");
        $input.val(url);
        $downForm.append($input);
        var $input1 = $("<input type='hidden'>");
        $input1.attr("name", "filename");
        $input1.val(realName);
        $downForm.append($input1);
        var $input2 = $("<input type='hidden'>");
        $input2.attr("name", "access_token");
        $input2.val(config.getToken().access_token);
        $downForm.append($input2);
        $(document.body).append($downForm);
        //提交表单，实现下载
        $downForm.submit();
    }

    //预览
    function previewImg(index) {
        stopBubbling(this);

        var obj = $(index).parent().prev().prev();
        var img = new Image();
        img.src = obj.attr("src");
        var height = img.height; //获取图片高度
        var width = img.width; //获取图片宽度
        if (width > 1000) {
            height = 1000 / width * height;
            width = 1000;
            if (height > 800) {
                width = 800 / height * width;
                height = 800;
            }
        }
        if (height > 800) {
            width = 800 / height * width;
            height = 800;
        }

        var imgHtml = "<img src='" + obj.attr("src") + "' style='width: " + width + "px; height: " + height + "px;' />";
        //弹出层
        layer.open({
            type: 1,
            shade: 0.8,
            offset: 'auto',
            area: [width + 'px', (height + 50) + 'px'],
            shadeClose: true,//点击外围关闭弹窗
            scrollbar: false,//不现实滚动条
            title: "图片预览", //不显示标题
            content: imgHtml, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
            cancel: function () {
                //layer.msg('捕获就是从页面已经存在的元素上，包裹layer的结构', { time: 5000, icon: 6 });
            }
        });
    }

    layui.use(['layer', 'form', 'table', 'admin', 'laytpl', 'upload'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var laytpl = layui.laytpl;

        // 渲染表格
        var insTb = table.render({
            elem: '#warehouseTable',
            url: config.base + 'warehouse/warehouse/admin/warehouseCustodyPrice/list',
            page: true,
            height: '315px',
            cols: [[
                 {field: 'custodyPriceName', align: 'center', title: '类型名称',width:150}
                , {field: 'custodyPriceCode', align: 'center', title: '类型编码',width:150 }
                , {field: 'price', align: 'center', title: '价格（单位：元/吨/天）'}
                , {field: 'remark', align: 'center', title: '备注说明' }
                // , {field: 'custodyPriceType', align: 'center', title: '是否接单'}

                ,{field:'custodyPriceType' ,title: '是否接单', align: 'center',  templet: function(d){
                        if(d.custodyPriceType === 0){
                            return '<span style="color: green; font-size: 18px">√</span>';
                        } else {
                            return '<span style="color: red;font-size: 20px">×</span>';
                        }
                    }}

                // ,{
                //     field: 'custodyPriceType',
                //     align: 'center',
                //     title: '是否接单',
                //     templet: function (d) {
                //         if (d.custodyPriceType === 0) {
                //             return '<input name="custodyPriceType" type="checkbox" lay-skin="switch" value="' + d.id + '" checked="' + d.custodyPriceType + '" lay-filter="custodyPriceType"  lay-text="是|否"/>';
                //         } else {
                //             return '<input name="custodyPriceType" type="checkbox" lay-skin="switch" value="' + d.id + '" lay-filter="custodyPriceType"   lay-text="是|否"/>';
                //         }
                //     },
                // }
                // , {align: 'center', toolbar: '#warehouseTableBar', title: '操作', fixed: 'right',width:150}
            ]]
        });

        // 工具条点击事件
        table.on('tool(warehouseTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(warehouseTable)', function (obj) {
            table.reload('warehouseTable', {
                initSort: obj
                , where: {
                    sort: obj.field
                    , order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            let field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
        });

        // 添加按钮点击事件
        $('#warehouseBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {

            console.info(data, 9090);

            admin.open({
                type: 1,
                area: '860px',
                offset: '120px',
                title: data ? '修改产品类型' : '添加产品类型',
                content: $('#warehouseForm').html(),
                success: async function () {

                    //产品类型
                    // admin.initDataSelect("enterprise/product/admin/productType/list",{limit:9999},"productTypeId",null,"产品类型","name",null);

                    await admin.initDataSelectPro(
                        'enterprise/product/admin/productType/list',
                        {
                            limit: 999,
                            // shipmentOrderStatus: '2',
                        },
                        'productTypeId',
                        '产品类型',
                        async (d, obj) => {
                            $('#warehouseProductCode').empty();
                            form.val('warehouseForm', {
                                custodyPriceCode: obj.code,
                                custodyPriceName: obj.name,
                            });
                        },
                        'name',
                    );



                    form.val('warehouseForm', data);
                    // 表单提交事件
                    form.on('submit(warehouseFormSubmit)', function (d) {
                        layer.load(2);
                        var url = 'warehouse/warehouse/admin/warehouseCustodyPrice/add';
                        if (data) {
                            url = 'warehouse/warehouse/admin/warehouseCustodyPrice/update';
                        }
                        admin.req(url,JSON.stringify(d.field) , function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('warehouseTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST',true,"application/json;charset=utf-8");
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('warehouse/warehouse/admin/warehouseCustodyPrice/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

    });
</script>